import { clone, isNil, sumBy } from "lodash-es"

export const table = {
    keyof<T>(arr:T[],value:T){
    return arr.includes(value)
    },
    indexof<T>(arr:T[],value:T){
       return arr.indexOf(value)
    },
    insert<T>(arr:T[],value:T){
        arr.push(value)
    },
    remove<T>(arr:T[],index:number){
       if(index >-1){
        arr.splice(index,1)
       }
    },
    copy<T,P extends T[]|Record<number,T>>(obj:P):P{
        if(obj instanceof Array){
           return obj.slice() as P
        } else {
            return clone(obj)
        }
    },
    length(arr:any[] | Record<any,any>){
        if(arr instanceof Array){
            return arr.filter(item=>item).length
        } else{
            return Object.keys(arr).length
        }
    
    },
    filter<T>(arr:T[],fn:(item:T,index:number)=>boolean){
      return arr.filter((item,index)=>{
        if(isNil(item)) return false
        return fn(item,index)
      })
    },
    count<T>(arr:T[]){
        return arr.length
      },
    sum:sumBy
}